package com.xk.androiddemo.ui.home;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.github.mikephil.charting.data.PieEntry;
import com.xk.androiddemo.NetActivity;
import com.xk.androiddemo.TActivity;
import com.xk.androiddemo.databinding.FragmentHomeBinding;
import com.xk.androiddemo.model.MonitoringData;
import com.xk.androiddemo.utils.UIUtils;


import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class HomeFragment extends Fragment {

    private FragmentHomeBinding binding;

    @RequiresApi(api = Build.VERSION_CODES.O)
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        HomeViewModel homeViewModel =
                new ViewModelProvider(this).get(HomeViewModel.class);
        homeViewModel.createData();
        binding = FragmentHomeBinding.inflate(inflater, container, false);
        View root = binding.getRoot();
        homeViewModel.monitoringDataMutableLiveData.observe(getActivity(), new Observer<MonitoringData>() {
            @Override
            public void onChanged(MonitoringData monitoringData) {
                binding.pressed.setText(monitoringData.getGasPressureValue()+"");
                binding.wendu.setText(monitoringData.getTemperatureValue()+"");
                binding.liuliang.setText(monitoringData.getFlowRateValue());
            }
        });
        LocalDateTime currentTime = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedDateTime = currentTime.format(formatter);
        binding.tvTime.setText(formattedDateTime);
        binding.clWen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent =new Intent(getActivity(), TActivity.class);
                startActivity(intent);
            }
        });
        binding.clLiu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent =new Intent(getActivity(), NetActivity.class);
                startActivity(intent);
            }
        });
        List<PieEntry> pieEntries = new ArrayList<>();
        pieEntries.add(new PieEntry(0.45f, "")); // 您已经添加的条目
        pieEntries.add(new PieEntry(0.55f, "")); // 新增一个占比为20%的条目
        UIUtils.showPieChart(binding.pieChart, pieEntries);
        return root;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}